/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import interfaces.BotObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jibble.pircbot.Colors;

import tools.StringTools;
import tools.UtopiaTime;
import database.DBConnectionFactory;

public class Status implements BotObject {
   /**
    * Returns a list of statuses for all players or the specified player
    * 
    * @param args
    * @return a List of Status objects
    */
   public static List<Status> getStatus(final String... args) {
	  List<Status> out = new LinkedList<Status>();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 if (args.length == 1) {
			ps = DBConnectionFactory.getConnectionHandler().getConnection()
				  .prepareStatement("SELECT * FROM status WHERE mainnick = ?");
			ps.setString(1, args[0]);
		 } else {
			ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM status");
		 }
		 rs = ps.executeQuery();

		 if (rs.next()) {
			out.add(new Status(rs.getString("mainnick"), rs.getString("statusmsg"), rs.getTimestamp("settime").getTime()));
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   private String nick = "";
   private String statusMessage = "";

   private Long time = 0L;

   public Status(final String nick, final String statusMessage, final Long time) {
	  this.nick = nick;
	  this.statusMessage = statusMessage;
	  this.time = time;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  List<String> out = new LinkedList<String>();
	  if (StringTools.isNullOrEmpty(getNick()) || getStatusMessage() == null || getTime() == null)
		 return out;
	  out.add(Colors.RED + getNick() + "'s status: " + Colors.OLIVE + getStatusMessage() + Colors.BLUE + ". Set: "
			+ UtopiaTime.compareTimes(getTime()) + " ago");
	  return out;
   }

   public String getNick() {
	  return nick;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  Map<String, Object> out = new HashMap<String, Object>();
	  if (StringTools.isNullOrEmpty(getNick()) || getStatusMessage() == null || getTime() == null)
		 return out;
	  out.put("Nick", getNick());
	  out.put("Set Time", new Date(getTime()));
	  out.put("Status", getStatus());
	  return out;
   }

   public String getStatusMessage() {
	  return statusMessage;
   }

   public Long getTime() {
	  return time;
   }

   @Override
   public boolean saveToDB() {
	  boolean out = false;
	  if (StringTools.isNullOrEmpty(getNick()) || getStatusMessage() == null || getTime() == null)
		 return out;
	  PreparedStatement ps = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection()
			   .prepareStatement("DELETE FROM status WHERE mainnick = ?");
		 ps.setString(1, getNick());
		 ps.executeUpdate();

		 try {
			ps.close();
		 } catch (final Exception e) {
		 }

		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("INSERT INTO status VALUES(?,?,?)");
		 ps.setString(1, getNick());
		 ps.setString(2, getStatusMessage());
		 ps.setTimestamp(3, new Timestamp(getTime()));
		 if (ps.executeUpdate() == 1) {
			out = true;
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }
}
